package com.shop.cereshop.app.service.buyer.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.dao.buyer.CereBuyerRelationshipDAO;
import com.shop.cereshop.app.dao.buyer.CereBuyerVisitorDAO;
import com.shop.cereshop.app.dao.buyer.CerePetSearchDAO;
import com.shop.cereshop.app.dao.collect.CereBuyerCollectDAO;
import com.shop.cereshop.app.dao.recommend.CereRecommendFootprintDAO;
import com.shop.cereshop.app.dao.recommend.CereRecommendLikesDAO;
import com.shop.cereshop.app.dao.recommend.CereRecommendTypeDAO;
import com.shop.cereshop.app.page.buyer.CollectAndLikeNumber;
import com.shop.cereshop.app.page.buyer.LikeAndCollectInfo;
import com.shop.cereshop.app.page.buyer.UserDetailVO;
import com.shop.cereshop.app.page.recommend.RecommendPageVO;
import com.shop.cereshop.app.param.buyer.BrowseHistory;
import com.shop.cereshop.app.param.buyer.BuyerRelationShipVO;
import com.shop.cereshop.app.param.buyer.CereBuyerRecord;
import com.shop.cereshop.app.param.recommend.LikeOrCollectReadParam;
import com.shop.cereshop.app.service.buyer.CereBuyerRelationShipService;
import com.shop.cereshop.app.service.recommend.CereRecommendDraftService;
import com.shop.cereshop.app.service.recommend.CereRecommendFootprintService;
import com.shop.cereshop.app.utils.RedisUtil;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerRelationship;
import com.shop.cereshop.commons.domain.buyer.CereBuyerVisitor;
import com.shop.cereshop.commons.domain.buyer.CerePetSearch;
import com.shop.cereshop.commons.domain.buyer.FollowUserVo;
import com.shop.cereshop.commons.domain.collect.CereBuyerCollect;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.common.PageParam;
import com.shop.cereshop.commons.domain.im.tx.req.follow.FollowCheckReq;
import com.shop.cereshop.commons.domain.im.tx.req.follow.FollowGetInfoReq;
import com.shop.cereshop.commons.domain.im.tx.req.friend.GetSpecifyFriendReq;
import com.shop.cereshop.commons.domain.im.tx.resp.follow.FollowGetInfoResp;
import com.shop.cereshop.commons.domain.im.tx.resp.follow.FollowUserResp;
import com.shop.cereshop.commons.domain.im.tx.resp.follow.GetSpecifyFriendResp;
import com.shop.cereshop.commons.domain.recommend.CereRecommendFootprint;
import com.shop.cereshop.commons.enums.RedisEnum;
import com.shop.cereshop.commons.enums.recommend.RecommendFootPrintEnum;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.service.im.ImUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CereBuyerRelationShipServiceImpl implements CereBuyerRelationShipService {

    @Autowired
    CereBuyerVisitorDAO cereBuyerVisitorDAO;

    @Autowired
    private CereBuyerRelationshipDAO cereBuyerRelationshipDAO;

    @Autowired
    private CerePetSearchDAO cerePetSearchDAO;

    @Autowired
    private CereBuyerCollectDAO cereBuyerCollectDAO;

    @Autowired
    private ImUserService imUserService;


    @Autowired
    private CereRecommendLikesDAO cereRecommendLikesDAO;

    @Autowired
    private CereRecommendTypeDAO cereRecommendTypeDAO;

    @Resource
    private CereRecommendFootprintService cereRecommendFootprintService;

    @Autowired
    private CereRecommendFootprintDAO cereRecommendFootprintDAO;

    @Autowired
    private CereRecommendDraftService cereRecommendDraftService;

    @Resource
    private RedisUtil redisUtil;


    @Override
    public void save(BuyerRelationShipVO buyerRelationShipVO, Long buyerUserId) {

        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        CereBuyerRelationship cereBuyerRelationship = new CereBuyerRelationship();
        cereBuyerRelationship.setConcernId(buyerUserId);
        cereBuyerRelationship.setPublishPostUserId(buyerRelationShipVO.getPublishPostUserId());
        cereBuyerRelationship.setConcernStatus(buyerRelationShipVO.getConcernStatus());
        cereBuyerRelationship.setCreateTime(currentTime);
        cereBuyerRelationship.setUpdateTime(currentTime);
        cereBuyerRelationshipDAO.insert(cereBuyerRelationship);
    }

    @Override
    public void updateBuyerRelationShip(BuyerRelationShipVO buyerRelationShipVO, Long buyerUserId) {
//        更改用户之间的关系
        LocalDateTime currentTime = LocalDateTime.now();
        CereBuyerRelationship cereBuyerRelationship = new CereBuyerRelationship();
        cereBuyerRelationship.setConcernId(buyerUserId);
        cereBuyerRelationship.setPublishPostUserId(buyerRelationShipVO.getPublishPostUserId());
        cereBuyerRelationship.setConcernStatus(buyerRelationShipVO.getConcernStatus());
        cereBuyerRelationship.setUpdateTime(currentTime);
        cereBuyerRelationshipDAO.updateStatus(cereBuyerRelationship);
    }

    @Override
    public UserDetailVO getUserInfo(Long buyerUserId, String visitedUserId) throws CoBusinessException {
//        根据用户id查询用户信息
        UserDetailVO userDetailVO = cereBuyerRelationshipDAO.selectUserInfo(visitedUserId);
        if (userDetailVO != null) {
            //增加用户访问和历史记录
            addVisit(userDetailVO, buyerUserId);
            Integer age = calculateAge(userDetailVO.getBirthday());
            //判断是否是个人中心
            if (visitedUserId.equals(buyerUserId.toString())) {
                userDetailVO.setPersonalCenter(1);
            } else {
                userDetailVO.setPersonalCenter(0);
//            如果不是自己的话，则不返回访客数量
                userDetailVO.setVisitedCount(-1);
                //        获取腾讯三方的用户之间的关系
                FollowCheckReq userRelationship = new FollowCheckReq();
                userRelationship.setFrom_Account(buyerUserId.toString());
                userRelationship.setTo_Account(new String[]{visitedUserId});
                FollowUserResp followUserResp = imUserService.followCheck(userRelationship);
                if (followUserResp != null &&
                        followUserResp.getActionStatus().equals("OK") &&
                        followUserResp.getResultItem() != null &&
                        !followUserResp.getResultItem().isEmpty()) {

                    List<FollowUserResp.FollowAdd> resultItems = followUserResp.getResultItem();
                    FollowUserResp.FollowAdd firstItem = resultItems.get(0);
                    Integer relationShipCode = firstItem.getFollowType();
//                判断当前用户和被访问者之间是什么关系
                    if (relationShipCode == null) {
                        userDetailVO.setRelation(-1);
                    } else if (relationShipCode == 1 || relationShipCode == 0) {
                        userDetailVO.setRelation(0);
                    } else if (relationShipCode == 2 || relationShipCode == 3) {
                        userDetailVO.setRelation(1);
                    }

                }
//            判断当前用户和被访问者之间是否是好友
                GetSpecifyFriendReq req = new GetSpecifyFriendReq();
                req.setFromAccount(buyerUserId.toString());
                req.setToAccount(new String[]{visitedUserId});
                GetSpecifyFriendResp getSpecifyFriendResp = imUserService.getSpecifyFriend(req);
                List<String> friendIdList = new ArrayList<>();
                if (StrUtil.equals("OK", getSpecifyFriendResp.getActionStatus())) {
                    if (CollUtil.isNotEmpty(getSpecifyFriendResp.getInfoItem())) {
                        friendIdList = getSpecifyFriendResp.getInfoItem().stream().filter(item -> item.getResultCode() == 0).map(GetSpecifyFriendResp.GetSpecifyFriendItem::getTo_Account).collect(Collectors.toList());
                    }
                    if (friendIdList.contains(visitedUserId)) {
                        userDetailVO.setIsFriend(1);
                    } else {
                        userDetailVO.setIsFriend(0);
                    }
                }
                System.out.println(getSpecifyFriendResp);

            }
//        获取腾讯三方粉丝数量,以及关注数量,互关数量
            FollowGetInfoReq followGetInfoReq = new FollowGetInfoReq();
            followGetInfoReq.setFrom_Account(buyerUserId.toString());
            followGetInfoReq.setTo_Account(new String[]{visitedUserId});
            FollowGetInfoResp userInfoCount = imUserService.getFollowInfo(followGetInfoReq);
            List<FollowGetInfoResp.FollowGetInfoItem> followInfo = userInfoCount.getFollowInfo();
            if (StrUtil.equals("OK", userInfoCount.getActionStatus()) && CollUtil.isNotEmpty(followInfo)) {
                FollowGetInfoResp.FollowGetInfoItem followGetInfoItem = followInfo.get(0);
                userDetailVO.setMutualConcernCount(Math.toIntExact(Long.valueOf(followGetInfoItem.getMutualFollowingCount())));
                userDetailVO.setFanCount(Math.toIntExact(Long.valueOf(followGetInfoItem.getFollowerCount())));
                userDetailVO.setConcernCount(Math.toIntExact(Long.valueOf(followGetInfoItem.getFollowingCount())));
            }
            userDetailVO.setAge(age);
        } else {
            throw new CoBusinessException(CoReturnFormat.USER_NOT_EXIST);
        }
        return userDetailVO;
    }

    @Async
    public void addVisit(UserDetailVO param, Long buyerUserId) {
        //判断是否存在访问记录和历史访问(自己不记录)
        if (!param.getBuyerUserId().equals(buyerUserId)) {
            CereBuyerRecord cereBuyerRecord = new CereBuyerRecord();
            cereBuyerRecord.setVisitedUserId(param.getBuyerUserId());
            cereBuyerRecord.setVisitorUserId(buyerUserId);
            addVisited(cereBuyerRecord);
            CereRecommendFootprint footprint = cereRecommendFootprintService.lambdaQuery()
                    .eq(CereRecommendFootprint::getTargetId, param.getBuyerUserId())
                    .eq(CereRecommendFootprint::getFootprintType, RecommendFootPrintEnum.USER.getCode())
                    .one();
            if (footprint != null) {
                // 正确的更新方式 - 使用条件更新，不包含主键字段
                cereRecommendFootprintService.lambdaUpdate()
                        .eq(CereRecommendFootprint::getRecommendFootprintId, footprint.getRecommendFootprintId())
                        .set(CereRecommendFootprint::getUpdateTime, LocalDateTime.now())
                        .update();
            } else {
                //不存在则插入
                CereRecommendFootprint footprint1 = new CereRecommendFootprint();
                footprint1.setTargetId(param.getBuyerUserId())
                        .setBuyerUserId(buyerUserId)
                        .setFootprintType(RecommendFootPrintEnum.USER.getCode());
                cereRecommendFootprintService.save(footprint1);
            }
        }
    }

    @Override
    public void addVisited(CereBuyerRecord cereBuyerRecord) {

        Long visitorUserId = cereBuyerRecord.getVisitorUserId();
        Long visitedUserId = cereBuyerRecord.getVisitedUserId();

        //判断是否存在访问记录
        CereBuyerVisitor existingRecord = cereBuyerRelationshipDAO.selectByVisitorAndVisited(visitorUserId, visitedUserId);
        LocalDateTime now = LocalDateTime.now();
        if (existingRecord == null) {

            // 如果不存在，则插入新记录
            CereBuyerVisitor record = new CereBuyerVisitor();
            record.setVisitorUserId(visitorUserId);
            record.setVisitedId(visitedUserId);
            record.setCreateTime(now);
            record.setUpdateTime(now);
            record.setVisitTime(now);
            record.setTimeupdateTime(now);
//            设置为未读
            record.setStatue(0);

            cereBuyerVisitorDAO.insert(record);
        } else {
            //如果存在访问记录，则更新该记录
            existingRecord.setStatue(0);
            existingRecord.setUpdateTime(now);
            existingRecord.setTimeupdateTime(now);
            cereBuyerVisitorDAO.updateStatue(existingRecord);
        }
    }

    @Override
    public Page getVisitedList(PageParam param, Long buyerUserId) throws CoBusinessException {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        Map<Long, Integer> relationshipMap = new HashMap<>();

//        更新所有被访问者的状态修改为已读状态
        cereBuyerVisitorDAO.updateVisitedStatue(buyerUserId);

        List<CereBuyerVisitor> visitorList = cereBuyerVisitorDAO.selectByVisitedId(buyerUserId);
        List<FollowUserVo> followUserVoList = new ArrayList<>();
        // 收集所有需要查询的用户ID
        if (visitorList != null && !visitorList.isEmpty()) {
            // 提取所有访问者的ID并去重
            String[] userVisitorUserIds = visitorList.stream()
                    .map(visitor -> visitor.getVisitorUserId().toString())
                    .distinct()
                    .toArray(String[]::new);

            // 批量查询用户关系
            FollowCheckReq userRelationship = new FollowCheckReq();
            userRelationship.setFrom_Account(buyerUserId.toString());
            userRelationship.setTo_Account(userVisitorUserIds);
            FollowUserResp followUserResp = imUserService.followCheck(userRelationship);
            // 构建关系映射Map，key为访问者ID，value为关系状态
            if (followUserResp != null &&
                    followUserResp.getActionStatus().equals("OK") &&
                    followUserResp.getResultItem() != null &&
                    !followUserResp.getResultItem().isEmpty()) {
                for (FollowUserResp.FollowAdd item : followUserResp.getResultItem()) {
                    // 检查 to_Account 是否为有效的数字字符串
                    Integer resultCode = item.getResultCode();
                    if (resultCode == 0) {
                        String toAccount = item.getTo_Account();
                        relationshipMap.put(Long.valueOf(toAccount), item.getFollowType());
                    } else {
                        throw new CoBusinessException(CoReturnFormat.USER_NOT_EXIST);
                    }
                }
            }
            FollowUserVo followUserVo = null;
            // 根据关系映射设置每个访客的关系状态
            for (CereBuyerVisitor visitor : visitorList) {
                Integer followType = relationshipMap.get(visitor.getVisitorUserId());
                /*if (followType != null) {
                    if (followType == 0 || followType == 1) {
                        visitor.setRelationShip("0");
                    } else if (followType == 2 || followType == 3) {
                        visitor.setRelationShip("1");
                    }
                }*/
                followUserVo = new FollowUserVo();
                followUserVo.setUserId(visitor.getVisitorUserId());
                followUserVo.setName(visitor.getName());
                followUserVo.setHeadImage(visitor.getHeadImage());
                if (null != followType) {
                    switch (followType) {
                        case 1:
                            followUserVo.setIsFans(1);
                            break;
                        case 2:
                            followUserVo.setIsFollow(1);
                            break;
                        case 3:
                            followUserVo.setIsMutual(1);
                            followUserVo.setIsFollow(1);
                            followUserVo.setIsFans(1);
                            break;
                    }
                }
                followUserVo.setTimes(visitor.getVisitTime());
                followUserVoList.add(followUserVo);
            }
        }

        PageInfo<FollowUserVo> pageInfo = new PageInfo<>(followUserVoList);
        Page page = new Page(pageInfo.getList(), pageInfo.getTotal());
        return page;
    }


    @Override
    public CereBuyerRelationship selectBuyerRelationShip(Long bUserId, Long publishPostUserId) {
        return cereBuyerRelationshipDAO.selectUserRelationship(bUserId, publishPostUserId);

    }

    @Override
    public Page selectHistoryPetSearch(PageParam param, Long buyerUserId) {
        PageHelper.startPage(param.getPage(), param.getPageSize());

        List<CerePetSearch> cerePetSearchList = cerePetSearchDAO.selectPetSearch(buyerUserId);

        PageInfo<CerePetSearch> pageInfo = new PageInfo<>(cerePetSearchList);
        Page page = new Page(pageInfo.getList(), pageInfo.getTotal());
        return page;
    }

    @Override
    public Integer deletePetSearch(List<Long> searchPetIds, Long buyerUserId) {
        LambdaQueryWrapper<CerePetSearch> queryWrapper = new LambdaQueryWrapper<>();

        // 添加搜索ID列表条件
        if (searchPetIds != null && !searchPetIds.isEmpty()) {
            queryWrapper.in(CerePetSearch::getSearchPetId, searchPetIds);
        }
        // 添加用户ID条件
        if (buyerUserId != null) {
            queryWrapper.eq(CerePetSearch::getSearchUserId, buyerUserId);
        }

        return cerePetSearchDAO.delete(queryWrapper);
    }

    @Override
    public void addPetSearch(String searchContent, Long buyerUserId) throws org.apache.ibatis.exceptions.TooManyResultsException {


        CerePetSearch cerePetSearchIsRepat = cerePetSearchDAO.selectPetSearchByContentAndUserId(searchContent, buyerUserId);
        if (cerePetSearchIsRepat == null) {
            // 增加宠圈搜索历史记录
            CerePetSearch cerePetSearch = new CerePetSearch();
            cerePetSearch.setSearchContent(searchContent);
            cerePetSearch.setSearchUserId(buyerUserId);
            cerePetSearch.setCreateTime(LocalDateTime.now());
            cerePetSearchDAO.insert(cerePetSearch);
        }
    }


    @Override
    public Page getCollect(PageParam param, Long buyerUserId) {
//        查询用户收藏的动态
        PageHelper.startPage(param.getPage(), param.getPageSize());

        List<CereBuyerCollect> collectList = cereBuyerCollectDAO.findByUserDaynamicS(buyerUserId);
        if (collectList != null && !collectList.isEmpty()) {
            collectList.forEach(collect -> {
                if (collect.getTopicName() == null || collect.getTopicName().isEmpty()) {
                    collect.setTopicName("流浪救助站");
                }
            });
        }
        PageInfo<CereBuyerCollect> pageInfo = new PageInfo<>(collectList);
        Page page = new Page(pageInfo.getList(), pageInfo.getTotal());
        return page;
    }

    @Override
    public CollectAndLikeNumber likeAndCollectCount(Long buyerUserId) {
        //  查询收藏未读数量
        Long collectCount = cereBuyerCollectDAO.findUserCollectCount(buyerUserId);

        //查询点赞的未读数量
        Long commentLikeCount = cereBuyerRelationshipDAO.findUserCommentLikeCount(buyerUserId);

        CollectAndLikeNumber collectAndLikeNumber = new CollectAndLikeNumber();
        collectAndLikeNumber.setCollectNumber(collectCount);
        collectAndLikeNumber.setLikeNumber(commentLikeCount);
        collectAndLikeNumber.setTotal(collectCount + commentLikeCount);
        return collectAndLikeNumber;
    }

    @Override
    public Page likeAndCollectList(PageParam param, Long buyerUserId) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<LikeAndCollectInfo> collectList = cereBuyerCollectDAO.selectCollectByUserId(buyerUserId);

        updateBuyerCollectAndLike(buyerUserId);

        PageInfo<LikeAndCollectInfo> pageInfo = new PageInfo<>(collectList);
        Page page = new Page(pageInfo.getList(), pageInfo.getTotal());
        return page;
    }

    @Override
    public boolean setReadLikeAndCollect(Long buyerUserId, LikeOrCollectReadParam readParam) {
        return false;
    }

    @Override
    public Page<RecommendPageVO> userBrowseHistory(BrowseHistory param) throws CoBusinessException {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        if (param.getType() == 1 || param.getType() == 2 || param.getType() == 0) {
            List<RecommendPageVO> list = cereRecommendFootprintDAO.selectUserBrowseHistory(param.getBuyerUserId(), param.getType(), param.getRecommendId());
//            返回分页结果,用户访问的动态结果
            PageInfo<RecommendPageVO> pageInfo = new PageInfo<>(list);
            Page page = new Page(pageInfo.getList(), pageInfo.getTotal());
            return page;
//          返回用户访问的用户结果
        } else if (param.getType() == 3) {
            List<RecommendPageVO> list = cereBuyerVisitorDAO.selectUserBrowseHistory(param.getBuyerUserId());
            Map<Long, Integer> relationshipMap = new HashMap<>();
//            判断当前用户和浏览记录中的用户是否关注
            if (list != null && !list.isEmpty()) {
                // 提取所有访问者的ID并去重
                String[] userVisitorUserIds = list.stream()
                        .map(visitor -> visitor.getPublisherId().toString())
                        .distinct()
                        .toArray(String[]::new);

                // 批量查询用户关系
                FollowCheckReq userRelationship = new FollowCheckReq();
                userRelationship.setFrom_Account(param.getBuyerUserId().toString());
                userRelationship.setTo_Account(userVisitorUserIds);
                FollowUserResp followUserResp = imUserService.followCheck(userRelationship);
                System.out.println(followUserResp);
                // 构建关系映射Map，key为访问者ID，value为关系状态
                if (followUserResp != null &&
                        followUserResp.getActionStatus().equals("OK") &&
                        followUserResp.getResultItem() != null &&
                        !followUserResp.getResultItem().isEmpty()) {

                    for (FollowUserResp.FollowAdd item : followUserResp.getResultItem()) {
                        Integer resultCode = item.getResultCode();
                        if (resultCode == 0) {
                            String toAccount = item.getTo_Account();
                            relationshipMap.put(Long.valueOf(toAccount), item.getFollowType());
                        } else {
                            throw new CoBusinessException(CoReturnFormat.USER_NOT_EXIST);
                        }


                    }
                }
                // 根据关系映射设置每个访客的关系状态
                for (RecommendPageVO visitor : list) {
                    Integer followType = relationshipMap.get(visitor.getPublisherId());
                    if (followType != null) {
                        if (followType == 0 || followType == 1) {
                            visitor.setConcernStatus(0);
                        } else if (followType == 2 || followType == 3) {
                            visitor.setConcernStatus(1);
                        }
                    }
                }

            }
            //            返回分页结果,用户访问的动态结果
            PageInfo<RecommendPageVO> pageInfo = new PageInfo<>(list);
            Page page = new Page(pageInfo.getList(), pageInfo.getTotal());
            return page;
        }
//        todo
        else if (param.getType() == 4) {
            return null;
        }
//        返回参数不合法
        throw new CoBusinessException(CoReturnFormat.PARAM_INVALID);
    }

    @Override
    public Integer deleteUserBrowseHistory(Integer type, Long buyerUserId) throws CoBusinessException {
        if (type == 1 || type == 2) {
            return cereBuyerVisitorDAO.deleteUserBrowseHistory(type, buyerUserId);
        }
        throw new CoBusinessException(CoReturnFormat.PARAM_INVALID);
    }

    @Override
    public Integer deleteUserHistory(List<Long> userIds, Long buyerUserId) {
        return cereBuyerVisitorDAO.deleteUserHistory(userIds, buyerUserId);

    }

    @Override
    public Integer deleteUserInfo(List<Long> recommendIds, Integer type, Long buyerUserId) {
//        删除收藏的动态
        if (type == 1) {
            if (CollUtil.isEmpty(recommendIds)) {
                recommendIds = cereBuyerCollectDAO.selectCollectIdsByUserId(buyerUserId);
            }
            for (Long recommendId : recommendIds) {
                redisUtil.decrementLike(RedisEnum.USER_COLLECT_KEY.getKey(), String.valueOf(recommendId));
            }
            return cereBuyerCollectDAO.deleteUserInfo(recommendIds, buyerUserId);
        }
//        删除赞过的动态
        else if (type == 2) {
            if (CollUtil.isEmpty(recommendIds)) {
                recommendIds = cereRecommendLikesDAO.selectCollectIdsByUserId(buyerUserId);
            }
            for (Long recommendId : recommendIds) {
                redisUtil.decrementLike(RedisEnum.USER_LIKE_KEY.getKey(), String.valueOf(recommendId));
            }
            return cereRecommendLikesDAO.deleteUserInfo(recommendIds, buyerUserId);
        }
//        删除足迹中的动态
        else if (type == 3) {
            return cereRecommendFootprintDAO.deleteUserInfo(recommendIds, buyerUserId);
        }
//        删除草稿箱
        else if (type == 4) {
            return cereRecommendDraftService.deleteUserInfo(recommendIds, buyerUserId);
        }
        return 0;
    }

    @Async
    public void updateBuyerCollectAndLike(Long buyerUserId) {
//        更新收藏表中的is_read设置为已读状态
        cereBuyerCollectDAO.updateIsReadByUserId(buyerUserId);

//        更新点赞表设置他的状态为已读状态
        cereRecommendLikesDAO.updateCommentLikeIsReadByUserId(buyerUserId);

    }


    /**
     * 计算年龄
     *
     * @param birthday 出生日期
     * @return 年龄
     */
    public static Integer calculateAge(String birthday) {
        try {
            // 解析出生日期
            if (birthday != null && !birthday.trim().isEmpty()) {
                LocalDate birthDate = LocalDate.parse(birthday, DateTimeFormatter.ISO_LOCAL_DATE);

                // 获取当前日期
                LocalDate currentDate = LocalDate.now();

                // 计算年龄
                return Period.between(birthDate, currentDate).getYears();
            } else {
                return -1;
            }
        } catch (DateTimeParseException e) {
            log.warn("日期格式解析失败: birthday={}", birthday, e);
            return -1;
        }
    }


}

